package com.promise.paging

import android.util.Log
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.*

/**
 * Created by CQNNJ-003 on 2018/6/5.
 */
interface Api {

    @GET("/r/subreddit/hot.json")
    fun getTopAfter( @Query("limit") limit: Int): Call<ListingResponse>


    class ListingResponse(val data: ListingData)

    class ListingData(
            val children: List<RedditChildrenResponse>,
            val after: String?,
            val before: String?
    )

    data class RedditChildrenResponse(val data: QuestsData)



    companion object {

        private const val BASE_URL = "https://www.reddit.com/"

        fun create(): Api = create(HttpUrl.parse(BASE_URL)!!)

        fun create(httpUrl: HttpUrl): Api {

            val logger = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
                Log.e("API", message)
            })
            logger.level = HttpLoggingInterceptor.Level.BODY
            val client = OkHttpClient.Builder()
                    .addInterceptor(logger)
                    .build()
            return Retrofit.Builder()
                    .baseUrl(httpUrl)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(Api::class.java)
        }

    }
}